﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace LightCAD.Three
{
    public class WebGLRenderState
    {
        public class State
        {
            public JsArr<Light> lightsArray;
            public JsArr<Light> shadowsArray;
            public WebGLLights lights;
        }
        private readonly WebGLLights lights;
        public readonly State state;
        private readonly JsArr<Light> lightsArray;
        private readonly JsArr<Light> shadowsArray;
        public WebGLRenderState(WebGLExtensions extensions, WebGLCapabilities capabilities)
        {
            this.lights = new WebGLLights(extensions, capabilities);
            this.lightsArray = new JsArr<Light>();
            this.shadowsArray = new JsArr<Light>();
            this.state = new State
            {
                lightsArray = lightsArray,
                shadowsArray = shadowsArray,
                lights = lights
            };
        }
        public void init()
        {

            lightsArray.length = 0;
            shadowsArray.length = 0;
        }

        public void pushLight(Light light)
        {

            lightsArray.push(light);

        }

        public void pushShadow(Light shadowLight)
        {

            shadowsArray.push(shadowLight);

        }

        public void setupLights(bool useLegacyLights)
        {
            lights.setup(lightsArray, useLegacyLights);
        }

        public void setupLightsView(Camera camera)
        {
            lights.setupView(lightsArray, camera);
        }
    }

    public class WebGLRenderStates : IDispose
    {
        private JsObj<Object3D, JsArr<WebGLRenderState>> renderStates;
        private WebGLExtensions extensions;
        private WebGLCapabilities capabilities;
        public WebGLRenderStates(WebGLExtensions extensions, WebGLCapabilities capabilities)
        {
            this.renderStates = new JsObj<Object3D, JsArr<WebGLRenderState>>();
            this.extensions = extensions;
            this.capabilities = capabilities;
        }
        public WebGLRenderState get(Object3D scene, int renderCallDepth = 0)
        {

            var renderStateArray = renderStates.get(scene);
            WebGLRenderState renderState;

            if (renderStateArray == null)
            {

                renderState = new WebGLRenderState(extensions, capabilities);
                renderStates.set(scene, new JsArr<WebGLRenderState> { renderState });

            }
            else
            {

                if (renderCallDepth >= renderStateArray.length)
                {

                    renderState = new WebGLRenderState(extensions, capabilities);
                    renderStateArray.push(renderState);

                }
                else
                {

                    renderState = renderStateArray[renderCallDepth];

                }

            }

            return renderState;

        }

        public void dispose()
        {

            renderStates = new JsObj<Object3D, JsArr<WebGLRenderState>>();
        }
    }
}
